<?php
/**
 * @package pmapi
 * @copyright Copyright (c)2014 Enrico Fagnoni
 * @license GNU General Public License version 3, or later
 *
 * @since 1.0.0
 */
 
defined('_JEXEC') or die;
 
if (!class_exists('VirtueMartModelOrders')) require ( JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'orders.php');
 
class PmapiModelOrders extends VirtueMartModelOrders
{   
    // redefine standard virtuemart default setPaginationLimits using com_pmapy style and default
    public function setPaginationLimits()
    {
        $jinput= JFactory::getApplication()->input;
        $page_limit = JComponentHelper::getParams('com_pmapi')->get('page_limit',1000);
        $this->_limit = $jinput->get('limit',$page_limit, 'uint');
        $this->_limitStart = $jinput->get('start',0, 'uint');
        
        return array($this->_limitStart,$this->_limit);
    }

  
    public function getItems()
    {
        $user = JFactory::getUser()->id;        
        if (!empty($user)){
            if(!class_exists('Permissions')) require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'permissions.php');
            if(Permissions::getInstance()->check('admin')) $user = 0;
            $orderList = $this->getOrdersList($user);
        } else {
            // if user is not logged returns an empty list
            $orderList = array();
        }
        
        // cleanup orderlist from unused fields
        foreach ($orderList as $key=>$order){
            $obj = new stdClass;
            $obj->virtuemart_order_id = $order->virtuemart_order_id;
            $orderList[$key]=$obj;
        }
       
        return $orderList;
    }        
}